import {action, computed, observable} from "mobx";
import {Bubble, Toast, Tools} from "weaver-mobile";
import {Browser, BrowserHrm, ListViewPage, TreePage} from "weaver-mobile-page";
import {cycleDataApi} from "../../api/performance/accessResultApi";
import {reportListApi} from "../../api/performance/reportApi";

const getLabel = Tools.getLabel;

/**
 * 考核报表
 * jiansong 2019/11/19
 */
export default class ReportStore {

    @observable orgStore = new TreePage.Store({route: "/performance/report/org"});
    @observable reportListStore = new ListViewPage.Store({route: "/performance/report"});
    @observable subBrowser = new Browser.Store({route: "/performance/report/condition"});
    @observable deptBrowser = new Browser.Store({route: "/performance/report/condition"});
    @observable hrmBrowser = new BrowserHrm.Store({route: "/performance/report/condition"});
    @observable subBrowserValue = [];
    @observable deptBrowserValue = [];
    @observable hrmBrowserValue = [];
    @observable stabList = []; //是否启用考核周期列表
    @observable hasInit = false; //是否已经初始化
    @observable showtype = "detail"; //detail详情/org组织机构

    //查询条件
    @observable queryParams = {
        type1: "1", //周期类型
        year: "",//年份
        year2: "", // 截止年份
        hrmids: "", //人员
        includesub: "3", //是否包含分部，默认含子分部
        subcompanyids: "", //分部
        includedept: "3", //是否包含部门，默认含子部门
        departmentids: "", //部门
        pageSize: 20, //每页显示数量
        current: 1, //当前页数
    }

    //字段数组
    @computed get fieldArray() {
        let arr = [];
        if (this.queryParams.type1 == 1) {
            return [
                {key: 1, value: 1, label: getLabel(25825, '1月')},
                {key: 2, value: 2, label: getLabel(25826, '2月')},
                {key: 3, value: 3, label: getLabel(25827, '3月')},
                {key: 4, value: 4, label: getLabel(25828, '4月')},
                {key: 5, value: 5, label: getLabel(25829, '5月')},
                {key: 6, value: 6, label: getLabel(25830, '6月')},
                {key: 7, value: 7, label: getLabel(25831, '7月')},
                {key: 8, value: 8, label: getLabel(25832, '8月')},
                {key: 9, value: 9, label: getLabel(25833, '9月')},
                {key: 10, value: 10, label: getLabel(25834, '10月')},
                {key: 11, value: 11, label: getLabel(25835, '11月')},
                {key: 12, value: 12, label: getLabel(25836, '12月')}
            ]
        } else if (this.queryParams.type1 == 2) {
            return [
                {key: 1, value: 1, label: getLabel(509092, '1季度')},
                {key: 2, value: 2, label: getLabel(509093, '2季度')},
                {key: 3, value: 3, label: getLabel(509094, '3季度')},
                {key: 4, value: 4, label: getLabel(509095, '4季度')}
            ];
        } else {
            let number = Number(this.queryParams.year) - Number(this.queryParams.year2);
            let rang = Math.abs(number)
            for (let i = 1, j = Number(this.queryParams.year); i <= rang + 1; i++, number <= 0 ? j++ : j--) {
                arr.push({
                    label: j + " " + (this.queryParams.type1 == 4 ? getLabel('17138', '年度') : getLabel('20729', '半年')),
                    value: i,
                    key: j
                });
            }
        }
        return arr;
    }

    @action("初始化操作")
    doInit = () => {
        //获取当前年份
        let date = new Date();
        let fullYear = date.getFullYear();
        let ydatas = [];
        for (let i = fullYear; i > 2012; i--) {
            ydatas.push({key: i, showname: i});
        }
        //默认选中当前年份
        this.yearDatas = ydatas;
        this.queryParams = {
            type1: "1",
            year: fullYear.toString(),
            year2: fullYear.toString(),
            hrmids: "",
            includesub: "3",
            subcompanyids: "",
            includedept: "3",
            departmentids: "",
            pageSize: 20,
            current: 1
        };
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.hrmBrowserValue = [];
        this.getCycleData();
    }

    @action("初始化数据")
    getCycleData = () => {
        cycleDataApi().then(action(res => {
            if (res.api_status) {
                this.stabList = res.stabList;
                this.hasInit = true;
                this.getTableDatas(false);
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }))
    }

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("重置条件")
    resetCondition = () => {
        this.queryParams = {
            ...this.queryParams,
            scorename: "",
            showstatus: "",
            hrmids: "",
            includesub: "3",
            subcompanyids: "",
            includedept: "3",
            departmentids: "",
            current: 1
        };
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.hrmBrowserValue = [];
    }

    @action
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }

    @action("渲染table数据")
    getTableDatas = (isSplice = true) => {
        reportListApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                //获取每行数据数量
                res.data && res.data.forEach((item, index, arr) => {
                    let num = 0;
                    if (this.queryParams.type1 == 1) {
                        for (let i = 1; i < 13; i++) {
                            if (item["result" + i]) num++;
                        }
                    } else if (this.queryParams.type1 == 2) {
                        for (let i = 1; i < 5; i++) {
                            if (item["result" + i]) num++;
                        }
                    } else {
                        let rang = Math.abs(Number(this.queryParams.year) - Number(this.queryParams.year2))
                        for (let i = 1; i <= rang + 1; i++) {
                            if (item["result" + i]) num++;
                        }
                    }
                    arr[index] = {...item, num}
                })
                const newState = {
                    isTable: false,
                    loading: false,
                    haveCheck: false,
                    template: {default: []},
                    hasMore: res.total > this.queryParams.current * this.queryParams.pageSize ? true : false,
                    data: isSplice ? (res.data && this.reportListStore.data.concat(res.data)) : (res.data || []),
                };
                this.reportListStore.setState(newState);
                this.reportListStore.updateDataSource();
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }))
    }

    @action('更新列表数据数据')
    updateListData = (params) => {
        const {data, updateDataSource, setState} = this.reportListStore;
        let index = data.findIndex(item => item.id == params.resourceid);
        if (index != undefined) {
            reportListApi({
                ...this.queryParams, current: index + 1, pageSize: 1
            }).then(action(res => {
                if (res.api_status) {
                    //获取每行数据数量
                    res.data && res.data.forEach((item, index, arr) => {
                        let num = 0;
                        if (this.queryParams.type1 == 1) {
                            for (let i = 1; i < 13; i++) {
                                if (item["result" + i]) num++;
                            }
                        } else if (this.queryParams.type1 == 2) {
                            for (let i = 1; i < 5; i++) {
                                if (item["result" + i]) num++;
                            }
                        } else {
                            let rang = Math.abs(Number(this.queryParams.year) - Number(this.queryParams.year2))
                            for (let i = 1; i <= rang + 1; i++) {
                                if (item["result" + i]) num++;
                            }
                        }
                        arr[index] = {...item, num}
                    })
                    if (res.data && res.data[0]) {
                        data[index] = res.data[0];
                        setState({data});
                        updateDataSource();
                    }
                }
            }))
        }
    }

}